package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

public class CouponListPagerAdapter extends FragmentPagerAdapter {
	
	FragmentManager mFm;
	List<Fragment> mTabContents;

	public CouponListPagerAdapter(FragmentManager fm,List<Fragment> mTabContents) {
		super(fm);
		mFm = fm;
		this.mTabContents = mTabContents;
	}

	@Override
	public Fragment getItem(int arg0) {
		return mTabContents.get(arg0);
	}

	@Override
	public int getCount() {
		return mTabContents.size();
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		super.destroyItem(container, position, object);
//		if(mTabContents.size() > 0) {
//			View view = mTabContents.get(position).getView();
//			container.removeView(view);
//		} else {
//			super.destroyItem(container, position, object);
//		}
		
//		if(object instanceof Fragment) {
//			FragmentTransaction transaction = mFm.beginTransaction();
//			transaction.remove((Fragment)object);
//			transaction.commitAllowingStateLoss();
//		}
	}
	
	public void destroySelf() {
		FragmentTransaction transaction = mFm.beginTransaction();
		if(mTabContents.size() > 0) {
			for(int i = 0;i < mTabContents.size();i++) {
				transaction.remove(mTabContents.get(i));
			}
		}
		transaction.commitAllowingStateLoss();
	}

}
